<template>
<div>
    <headerNav :title="title"/>
    <div class="address">
        <van-address-list
            v-model="chosenAddressId"
            :class="isSelect?'':'hideselect'"
            :list="list"
            :add-button-text="buttonText"
            @add="onAdd"
            @edit="onEdit"
            @select="onSelect"
        />
    </div>
    
</div>
</template>

<script>

import { findAddrList, findAddrById, getCurrentUser } from "../../../api/page.js";
import { AddressList } from 'vant';
export default {
    components:{
        [AddressList.name]:AddressList,
    },
    data() {
        return {
            chosenAddressId: 0,
            isSelect:false,
            title: "客户地址",
            buttonText: "新增客户",
            list: [],
            selected: false
        }
    },
    computed:{
        // 计算属性
        //chosenAddressId:function() {
        //    return this.$store.getters.getsAddressId;
        //}
        // getAddressId() {
        //     this.chosenAddressId = this.$store.getters.getsAddressId;
        // }
    },
    methods: {
        // 新增
        onAdd() {
            if (this.$route.query.type === "choose") {
                // 选择默认地址
                if (!this.selected) {
                    this.$store.dispatch("chooseAddressData", this.list[0]);
                }
                this.$router.go(-1)
                return
            }
            this.$router.push({name:"addressEdit",query:{type:"address"}})
        },
        // 修改
        onEdit(item) {
            let address = {
                address:  item.address,
                cityName:  item.cityName,
                districtName:  item.districtName,
                name:  item.name,
                provinceName:  item.provinceName,
                phoneNum:  item.phoneNum,
                areaCode : item.areaCode,
                inviteUserNo:  item.inviteUserNo || "",
                inviteUserName:  item.inviteUserName || "",
                compNo:  item.compNo || "",
                compName:  item.compName || "",
                userNo:  item.userNo,
                userName:  item.userName
            }
            localStorage.setItem("address",JSON.stringify(address));
            this.$router.push({name:"addressEdit",query:{id:item.id,type:"edit"}}); 
        },
        // 存默认地址
        onSelect(item){
            this.selected = true
            if (this.$route.query.type === "choose") {
                this.$store.dispatch("chooseAddressData", item);
            }
            this.$store.dispatch("setAddressData", item);
            this.$store.dispatch("setAddressDataid", item.id);
            this.chosenAddressId = item.id
            if(this.$route.query.type=="add"){
                this.$router.go(-1)
            }
        }
    },
    created(){
        if (this.$route.query.type === "choose") {
            this.title = "选择客户"
            this.buttonText = "确定"
        }
        // 获取地址列表
        findAddrList({
            pageNum: 0,
            pageSize: 10
        }).then(res=>{
            this.list = [];
            let dataList = res.data.list;
            this.list = dataList.map(item=>{
                return {
                    id: item.id,
                 //   address:  item.provinceName+item.cityName+item.districtName+item.contactAddr,
                    address:  item.contactAddr,
                    name:  item.userName,
                    districtName:  item.districtName,
                    cityName:  item.cityName,
                    provinceName:  item.provinceName,
                    districtCode:  item.districtCode,
                    cityCode:  item.cityCode,
                    provinceCode:  item.provinceCode,
                    phoneNum:  item.phoneNum,
                     tel:  item.tel,
                    areaCode : item.districtCode,
                    inviteUserNo:  item.inviteUserNo || "",
                    inviteUserName:  item.inviteUserName || "",
                    compNo:  item.compNo || "",
                    compName:  item.compName || "",
                    userNo: item.userNo
                }
            })
            this.chosenAddressId = this.$store.getters.getsAddressId
            if(this.chosenAddressId == null){
                this.$store.dispatch("setAddressData", this.list[0]);
                this.$store.dispatch("setAddressDataid", this.list[0].id);
                this.chosenAddressId = this.list[0].id
            }
        })
    },
    beforeRouteLeave(to, from, next) {
        if (to.path === "/order") {
            to.meta.keepAlive = true
        } else {
            to.meta.keepAlive = false
        }
        next()
    }

}
</script>

<style lang="less" scoped>
/deep/ .van-address-list__add {
    position: fixed;
    left: 0.4rem;
    width: 9.2rem;
    bottom: 0.853333rem;
    z-index: 9999;
    border-radius: 0.64rem;
    background: #FF531A;
    color: #fff;
}
.address{
    padding-top: 1.066667rem;
}
</style>
